//
//  LianTongSF-pch.swift
//  LianTongSF
//
//  Created by Visitor on 2017/7/7.
//  Copyright © 2017年 Eversec. All rights reserved.
//

import UIKit

///字典转换为JSONString
func jsonStrWithDict(dict: Dictionary<String, Any>) -> String {
    if (!JSONSerialization.isValidJSONObject(dict)) {
        print("无法解析出JSONString")
        return ""
    }
    let data : NSData! = try? JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions(rawValue: 0)) as NSData!
    let JSONString = NSString(data:data as Data,encoding: String.Encoding.utf8.rawValue)
    return JSONString! as String
}

/// JSONString转换为字典
func dictionaryFromeJSONString(jsonStr: String) ->NSDictionary{
    let jsonData:Data = jsonStr.data(using: .utf8)!
    
    let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
    if dict != nil {
        return dict as! NSDictionary
    }
    return NSDictionary()
}

///时间戳转当前时间
func stampToCurrentTime(stamp: Int) -> String {
    let timeInterval:TimeInterval = TimeInterval(stamp)
    let date = Date(timeIntervalSince1970: timeInterval)
    let dformatter = DateFormatter()
    dformatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    return dformatter.string(from: date)
}

///时间戳
func getTimeStamp() ->String{
    let now = Date()
    let timeInterval:TimeInterval = now.timeIntervalSince1970 * 1000
    let timeStamp = String(format: "%.0f", timeInterval)
    return timeStamp
}

//当前时间
func getCurrentDate() -> String {
    let formatter:DateFormatter = DateFormatter()
    formatter.timeZone = TimeZone(secondsFromGMT: +28800)
    formatter.dateFormat = "yyyy-MM-dd  HH:mm:ss"
    let str:String = formatter.string(from: Date())
    return str
}

enum ValidatedType {
    case Email
    case PhoneNumber
}
func ValidateText(validatedType type: ValidatedType, validateString: String) -> Bool {
    do {
        let pattern: String
        if type == ValidatedType.Email {
            pattern = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
        }
        else {
            pattern = "^1[0-9]{10}$"
        }
        
        let regex: NSRegularExpression = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
        let matches = regex.matches(in: validateString, options: NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, validateString.characters.count))
        return matches.count > 0
    }
    catch {
        return false
    }
}

func EmailIsValidated(vStr: String) -> Bool {
    return ValidateText(validatedType: ValidatedType.Email, validateString: vStr)
}
func PhoneNumberIsValidated(vStr: String) -> Bool {
    return ValidateText(validatedType: ValidatedType.PhoneNumber, validateString: vStr)
}

func  getParmsDict() -> Dictionary<String, Any>{
    var dict = [String: Any]()
    //deviceld()
    //7d3ea2ed8d6b5a7cd3b1fe3dc608e1e9
    dict["key"] = deviceld()
    dict["imsi"] = deviceld()
    dict["imei"] = deviceld()
    dict["versionName"] = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
    
    let versionCodeNum = (Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! NSString).components(separatedBy: ".")
    let versionCode = (versionCodeNum as NSArray).componentsJoined(by: "")
    let codeStr = NSNumber(value: Int(versionCode)!)
    dict["versionCode"] = codeStr
    dict["platform"] = "iOS"
    dict["model"] = UIDevice.current.platformString()
    dict["channel"] = "AppStore"
    dict["brand"] = "client"
    dict["product"] = "06"
    ////        Bundle.main.bundleIdentifier
    dict["appId"] = "com.hajx.testForPush3"
    dict["versionOS"] = UIDevice.current.systemVersion
    dict["manufacturer"] = "Apple"
    return dict
}


func deviceld() ->String {
    let device = UIDevice.current.uniqueDeviceIdentifier() as String
    return device
}

func IOS7() ->Bool {
    return (UIDevice.current.systemVersion as NSString).doubleValue >= 7.0
}

func IOS8() -> Bool {
    return (UIDevice.current.systemVersion as NSString).doubleValue >= 8.0
}

func IOS10() -> Bool {
    return (UIDevice.current.systemVersion as NSString).doubleValue >= 10.0
}


func RGB (r:CGFloat, g:CGFloat, b:CGFloat) ->UIColor {
    return UIColor (red: (r)/255.0, green: (g)/255.0, blue: (b)/255.0, alpha: 1.0)
}

func RGBA (r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat) ->UIColor {
    return UIColor (red: (r)/255.0, green: (g)/255.0, blue: (b)/255.0, alpha: a)
}

func RGB_S (c:CGFloat) ->UIColor {
    return UIColor (red: (c)/255.0, green: (c)/255.0, blue: (c)/255.0, alpha: 1.0)
}

func RandomColor () ->UIColor {
    return RGB(r: CGFloat(arc4random_uniform(255)), g: CGFloat(arc4random_uniform(255)), b: CGFloat(arc4random_uniform(255)))
}

func FONT(font: CGFloat) ->UIFont {
    return UIFont.systemFont(ofSize: font)
}

func LTYELLOW() -> UIColor {
    return RGB(r: 246.0, g: 129.0, b: 39.0)
}

func B_FONT(font: CGFloat) ->UIFont
{
    return UIFont.boldSystemFont(ofSize: font)
}
let M_S = UIScreen.main.bounds.size
let LINECOLOR = RGB_S(c: 238)
let SYSTEM = UIDevice.current.systemVersion
let DEVICE = UIDevice.current.model
let TXBule = RGB(r: 30.0, g: 127.0, b: 239.0)

